﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Common;


namespace ZhaoYan.enemy
{
    public class WeaponMono : MonoBehaviour
    {

        public void Start ()
        {
        }

        public void OnTriggerEnter (Collider other)
        {
            if (OnHit != null)
            {
                OnHit (other);
            }
        }

        internal event Action<Collider> OnHit;
    }

    class Weapon
    {
        WeaponMono _mono;

        public Weapon (GameObject go)
        {
            GameObject we = go;
            we.AddComponent (typeof (BoxCollider));
            Rigidbody rd = we.AddComponent<Rigidbody> ();
            rd.isKinematic = true;
            rd.useGravity = false;
            we.collider.isTrigger = true;

            _mono = we.AddComponent (typeof (WeaponMono)) as WeaponMono;

            _mono.OnHit += new Action<Collider> (_mono_OnHit);
        }

        bool _hitted = false;

        void _mono_OnHit (Collider obj)
        {
            if (_currentAttack != null)
            {
                _hitted = true;
            }
        }

        IAttack _currentAttack = null;

        public void BeginAttack (IAttack att)
        {
            _currentAttack = att;
        }

        public void EndAttack ()
        {
            if (OnHit != null && _hitted)
            {
                OnHit (this, new int[] {0}, _currentAttack);
            }
            _currentAttack = null;
        }

        public event HitHandler OnHit;
    }
}
